using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

/*
********************************************************************************
'
' Copyright (c) 2003, Robert Cowham and Vaccaperna Systems Ltd.  All rights reserved.
'
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions are met:
'
' 1.  Redistributions of source code must retain the above copyright
'     notice, this list of conditions and the following disclaimer.
'
' 2.  Redistributions in binary form must reproduce the above copyright
'     notice, this list of conditions and the following disclaimer in the
'     documentation and/or other materials provided with the distribution.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
' ARE DISCLAIMED. IN NO EVENT SHALL VACCAPERNA SYSTEMS LTD. BE LIABLE FOR ANY
' DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
' (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
' ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
' (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
' SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'
' *******************************************************************************
'
' Name:		submitform.cs
'
' Author:   Robert Cowham <robert@vaccaperna.co.uk>
'
' Description:
'   Show how to execute a command such as "change -o" and then "submit -i"
'   and allow the user to manipulate things in between.
'
'   Note there job lists available etc (ignored in this demo).
'
'   No error handling!
' ******************************************************************************
*/

namespace p4comtest
{
	/// <summary>
	/// Summary description for SubmitForm.
	/// </summary>
	public class SubmitForm : System.Windows.Forms.Form
	{
		public System.Windows.Forms.TextBox Description;
		public System.Windows.Forms.Button cmdClose;
		public System.Windows.Forms.Button cmdSubmit;
		public System.Windows.Forms.CheckedListBox Files;
		public System.Windows.Forms.Label _Label1_1;
		public System.Windows.Forms.Label _Label1_0;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SubmitForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Description = new System.Windows.Forms.TextBox();
			this.cmdClose = new System.Windows.Forms.Button();
			this.cmdSubmit = new System.Windows.Forms.Button();
			this.Files = new System.Windows.Forms.CheckedListBox();
			this._Label1_1 = new System.Windows.Forms.Label();
			this._Label1_0 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// Description
			// 
			this.Description.AcceptsReturn = true;
			this.Description.AutoSize = false;
			this.Description.BackColor = System.Drawing.SystemColors.Window;
			this.Description.Cursor = System.Windows.Forms.Cursors.IBeam;
			this.Description.ForeColor = System.Drawing.SystemColors.WindowText;
			this.Description.Location = new System.Drawing.Point(88, 8);
			this.Description.MaxLength = 0;
			this.Description.Name = "Description";
			this.Description.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Description.Size = new System.Drawing.Size(489, 105);
			this.Description.TabIndex = 10;
			this.Description.Text = "Description";
			// 
			// cmdClose
			// 
			this.cmdClose.BackColor = System.Drawing.SystemColors.Control;
			this.cmdClose.Cursor = System.Windows.Forms.Cursors.Default;
			this.cmdClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdClose.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmdClose.Location = new System.Drawing.Point(312, 280);
			this.cmdClose.Name = "cmdClose";
			this.cmdClose.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.cmdClose.Size = new System.Drawing.Size(97, 33);
			this.cmdClose.TabIndex = 9;
			this.cmdClose.Text = "Cancel";
			this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
			// 
			// cmdSubmit
			// 
			this.cmdSubmit.BackColor = System.Drawing.SystemColors.Control;
			this.cmdSubmit.Cursor = System.Windows.Forms.Cursors.Default;
			this.cmdSubmit.ForeColor = System.Drawing.SystemColors.ControlText;
			this.cmdSubmit.Location = new System.Drawing.Point(200, 280);
			this.cmdSubmit.Name = "cmdSubmit";
			this.cmdSubmit.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.cmdSubmit.Size = new System.Drawing.Size(97, 33);
			this.cmdSubmit.TabIndex = 8;
			this.cmdSubmit.Text = "&Submit";
			this.cmdSubmit.Click += new System.EventHandler(this.cmdSubmit_Click);
			// 
			// Files
			// 
			this.Files.BackColor = System.Drawing.SystemColors.Window;
			this.Files.Cursor = System.Windows.Forms.Cursors.Default;
			this.Files.ForeColor = System.Drawing.SystemColors.WindowText;
			this.Files.Items.AddRange(new object[] {
													   "Files"});
			this.Files.Location = new System.Drawing.Point(88, 128);
			this.Files.Name = "Files";
			this.Files.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this.Files.Size = new System.Drawing.Size(489, 139);
			this.Files.TabIndex = 6;
			// 
			// _Label1_1
			// 
			this._Label1_1.BackColor = System.Drawing.SystemColors.Control;
			this._Label1_1.Cursor = System.Windows.Forms.Cursors.Default;
			this._Label1_1.ForeColor = System.Drawing.SystemColors.ControlText;
			this._Label1_1.Location = new System.Drawing.Point(8, 8);
			this._Label1_1.Name = "_Label1_1";
			this._Label1_1.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this._Label1_1.Size = new System.Drawing.Size(65, 25);
			this._Label1_1.TabIndex = 11;
			this._Label1_1.Text = "Description:";
			this._Label1_1.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// _Label1_0
			// 
			this._Label1_0.BackColor = System.Drawing.SystemColors.Control;
			this._Label1_0.Cursor = System.Windows.Forms.Cursors.Default;
			this._Label1_0.ForeColor = System.Drawing.SystemColors.ControlText;
			this._Label1_0.Location = new System.Drawing.Point(8, 128);
			this._Label1_0.Name = "_Label1_0";
			this._Label1_0.RightToLeft = System.Windows.Forms.RightToLeft.No;
			this._Label1_0.Size = new System.Drawing.Size(65, 25);
			this._Label1_0.TabIndex = 7;
			this._Label1_0.Text = "Files:";
			this._Label1_0.TextAlign = System.Drawing.ContentAlignment.TopRight;
			// 
			// SubmitForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(592, 318);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.Description,
																		  this.cmdClose,
																		  this.cmdSubmit,
																		  this.Files,
																		  this._Label1_1,
																		  this._Label1_0});
			this.Name = "SubmitForm";
			this.Text = "SubmitForm";
			this.ResumeLayout(false);

		}
		#endregion

		P4COM.p4 m_p4 = new P4COM.p4();
		System.Array output;
		System.Array warnings;
		System.Array errors;
		System.Array m_Files;

		public void DoSubmit(P4COM.p4 p)
		{
			int i;
		
			/*
			 * Demonstrates how to handle commands which take -o and -i flags,
			 * in this case "change -o" and "submit -i" to commit the results.
			 * 
			 * Copy environment since we want to set ParseForms on for this connection
			 * */
			m_p4 = new P4COM.p4();
			m_p4.Charset = p.Charset;
			m_p4.Client = p.Client;
			m_p4.Cwd = p.Cwd;
			m_p4.Host = p.Host;
			m_p4.Language = p.Language;
			m_p4.Password = p.Password;
			m_p4.Port = p.Port;
			m_p4.User = p.User;
		
			m_p4.ParseForms();
			m_p4.Connect();
			output = m_p4.run("change -o");
		
			Description.Text = m_p4.get_Var("Description"); // Simple string variable
			m_Files = m_p4.get_ArrayVar("Files"); // StringArray variable
			Files.Items.Clear();
			for (i = m_Files.GetLowerBound(0); i <= m_Files.GetUpperBound(0); i++)
			{
				Files.Items.Add(m_Files.GetValue(i));
			}
			ShowDialog();
			Close();
		}

		private void cmdClose_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void cmdSubmit_Click(object sender, System.EventArgs e)
		{
			int i, count;
		
			m_p4.set_Var("Description", Description.Text); // Setting string var
			count = 0;
			for (i = 0; i < Files.Items.Count; i++)
			{
				if (Files.GetItemChecked(i))
				{
					count++;
				}
			}
			if (count == 0)
			{
				MessageBox.Show("No files selected!");
			}
			else
			{
				// There must be a better way of resetting m_Files than this
				// but I haven't found it yet!!
				string[] arr = new string[count];
				int j = 0;
				foreach(object item in Files.CheckedItems) 
				{
					arr[j++] = item.ToString();
				}
				m_Files = arr;
				//MessageBox.Show("Files: " + join(m_Files));

				m_p4.set_ArrayVar("Files", ref m_Files);
				output = m_p4.run("submit -i");
				errors = m_p4.Errors;

				string s = join(output);
				s = s + "\n" + "Errors: " + join(errors);
				MessageBox.Show(s);
			}
			Close();
		}

		private string join(System.Array arr)
		{
			int i;
			string s = "";
			for (i = arr.GetLowerBound(0); i <= arr.GetUpperBound(0); i++)
			{
				s += arr.GetValue(i) + " ";
			}
			return s;
		}

	}
}
